<?php

require_once('conf/DBConnection.php');
require_once('dao/DAO.php');

class ProvinciasDAO extends DAO {

	function ProvinciasDAO() {}


	function get($id){
		DBConnection::getInstance();
		$query = "SELECT * FROM provincias WHERE id = $id";
		$result = mysql_query($query);
		$results = $this->getFromResult($result);
		return $results[0];
	}

	function find($criteria){
		DBConnection::getInstance();
		$conditions = $this->getConditions($criteria);
		$query = "SELECT * FROM provincias WHERE 1=1 $conditions";
		$result = mysql_query($query);
		return $this->getFromResult($result);
	}

	function deleteFisico($id){
		DBConnection::getInstance();
		$query = "DELETE FROM provincias WHERE id = $id";
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function delete($id){
		DBConnection::getInstance();
		$query = "UPDATE provincias SET borrado = '1' WHERE id = $id";
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function insert($vo) {
		DBConnection::getInstance();
		$query = "INSERT INTO provincias(id, provincia, provincia2, borrado) VALUES
('".$vo->getId()."', '".$vo->getProvincia()."', '".$vo->getProvincia2()."', '".$vo->getBorrado()."')";
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function update($vo) {
		DBConnection::getInstance();
		$query = "UPDATE provincias SET ".
				"provincia ='".$vo->getProvincia()."', ".
				"provincia2 ='".$vo->getProvincia2()."' ".
				" WHERE id =".$vo->getId();
		$result = mysql_query($query);
		return parent::checkError($result);
	}

	function getConditions($criteria){
		if ($criteria->getProvincia() != null) {
			$conditions.= " AND provincia= '".$criteria->getProvincia()."'";
		}
		if ($criteria->getProvincia2() != null) {
			$conditions.= " AND provincia2= '".$criteria->getProvincia2()."'";
		}
		$conditions .= parent::getConditionsAbs($criteria,'provincias');
		return $conditions;
	}

}

